﻿let testTeacherData = {
  code: 1,
  msg: "xxxx",
  teachers: [
    {
      id: "1",
      name: "李华",
      sex: "1",
      teacher_title: "高级教师",
      faculty: "计信学院",
      intro: "非常强的教师",
      is_deleted: "0"
    },
    {
      id: "2",
      name: "博丽灵梦",
      sex: "2",
      teacher_title: "高级法师",
      faculty: "幻想乡",
      intro: "非常强的巫女",
      is_deleted: "0"
    }
  ]
}

let testTeacherCourseData = {
  code: 1,		//状态码
  msg: "xxxx",	//信息
  courses: [
    {
      cour_id: "1",				//课程号
      title: "机组",						//课程标题
      credit: "2",						//课程学分
      hour: "48",						//课程学时
      attribute: "1",			//课程性质（公选or专选
      proportion: "0.4",				//成绩占比
      faculty: "计信学院",					//院系
      comment: "很好的一门课",				//简介
      cover: "./static/xxx",				//课程图片
      status: "1",							//课程状态
      is_deleted: "0"   					//是否删除
    }
  ]
};

window.onload = function () {
  //获取课程信息列表
  getTeacherList();
  getCourseList();
}

//获取教师表信息
function getTeacherList() {
  //请求
  $.get("cotro/admin/getTeacher.ashx", function (res, status) {
    //res = testTeacherData;
    res = JSON.parse(res);
    //判断是否有数据,没有就返回
    if (res.code == -1 || res.teachers == null) {
      return;
    }
    //获取table对象
    let teacherTable = document.getElementsByClassName("teacher-warp");
    teacherTable[0].innerHTML = `
    <thead>
                <tr>
                    <th scope="col">教师号</th>
                    <th scope="col">姓名</th>
                    <th scope="col">性别</th>
                    <th scope="col">职称</th>
                    <th scope="col">所属学院</th>
                    <th scope="col">所教课程</th>
                    <th scope="col">详细信息</th>
                </tr>
    </thead>
    `
    //存放至全局
    window.teachers = res.teachers;
    //TODO
    teacherTable[0].innerHTML += addTeacherListToHTML(res.teachers);
  })
}

//获取课程信息列表
function getCourseList() {
  //获取所有的课程信息
  $.get("cotro/admin/getCourse.ashx", function (res, status) {
    res = JSON.parse(res);

    if (res.code != 1) {
      alert("未知错误")
      return;
    }
    window.allCourses = res.courses;
  })
}

//将教师表信息转换为HTML
function addTeacherListToHTML(teachers) {
  let s = '<tbody>'
  for (let i = 0; i < teachers.length; ++i) {
    if (teachers.is_deleted == 1) continue;
    s += `
          <tr>
              <td>${teachers[i].id}</td>
              <td>${teachers[i].name}</td>
              <td>${teachers[i].sex == "1" ? "男" : (teachers[i].sex == "2" ? "女" : "未知")}</td>
              <td>${teachers[i].teacher_title}</td>
              <td>${teachers[i].faculty}</td>
              <td><a id="teacher-course-info"  href="#"  onclick="getTeacherCourse(${teachers[i].id})" tabindex="-1" aria-disabled="true" data-toggle="modal" data-target="#teacherCourseModal">所教课程</a></td>
              <td><a id="teacher-info"  href="#"  onclick="getTeacherInfo(${teachers[i].id})" tabindex="-1" aria-disabled="true" data-toggle="modal" data-target="#teacherInfoModal">详细信息</a></td>
           </tr>
    `;
  }
  s += '</tbody>';
  return s;
}

//获取教师所教课程信息
function getTeacherCourse(tid) {
  window.tid = tid;
  $.post("cotro/admin/getTeacherCourse.ashx", {
    tea_id: tid
  }, function (res, status) {
    //res = testTeacherCourseData;
    //console.log(res);
    res = JSON.parse(res);
    if (res.code == -1 || res.courses == null) {
      return;
    }
    //将信息添加在模态框上面
    //console.log(res.courses);
    window.haveCourses = res.courses;
    addTeacherCourseInfoToModal(res.courses);
    teacher_course_model_title = document.getElementById("teacher-course-model-title");
    for (let i = 0, len = teachers.length; i < len; ++i) {
      if (teachers[i].id == window.tid) {
        teacher_course_model_title.innerHTML = `教师：${teachers[i].name}`;
      }
    }
  })
}

//将教师所教课程列表信息添加在模态框上
function addTeacherCourseInfoToModal(courses) {

  teacherCourse_table = document.getElementById("teacherCourse-table");
  teacherCourse_table.innerHTML = `
    <thead>
        <tr>
            <th scope="col">课程编号</th>
            <th scope="col">课程名称</th>
            <th scope="col">详细信息</th>
            <th scope="col">取消课程</th>
        </tr>
    </thead>
   `
  s = `<tbody>`

  for (let i = 0, len = courses.length; i < len; ++i) {
    if (courses.is_deleted == 1) continue;
    s += `
      <tr id="teacherCourse-c${courses[i].cour_id}">
        <th scope="row">${courses[i].cour_id}</th>
        <td>${courses[i].title}</td>
        <td><a href="javascript:void(0) onclick="teacherCourseDetail(${courses[i].cour_id})">详细信息</a></td>
        <td><a style="color: red;" href="javascript:void(0)" onclick="deleteTeacherCourse(${courses[i].cour_id})">取消课程</a></td>
      </tr>
    `
  }
  s += `</tbody>`
  teacherCourse_table.innerHTML += s;
}

//添加当前教师关联课程
function addTeacherCourse() {
  //将该教师未交课程全部显示在表格上
  showCourse_table = document.getElementById("showCourse-table");
  showCourse_table.innerHTML = `<thead>
        <tr>
            <th scope="col">课程编号</th>
            <th scope="col">课程名称</th>
            <th scope="col">选择框</th>
        </tr>
    </thead>`
  s = `</tbody>`
  let len = window.allCourses.length;
  console.log(window.allCourses);
  console.log(window.haveCourses);
  for (let i = 0; i < len; ++i) {
    let isOk = 0;
    for (let k = 0, len = window.haveCourses.length; k < len; k++) {
      if (window.allCourses[i].cour_id == window.haveCourses[k].cour_id) {
        isOk = 1;
        break;
      }
    }
    if (window.allCourses[i].is_deleted == 1 || isOk==1) continue;
    //添加表格内容
    s += `
      <tr id="teacherCourse-c${window.allCourses[i].cour_id}">
        <th scope="row">${window.allCourses[i].cour_id}</th>
        <td>${window.allCourses[i].title}</td>
        <td><input type="checkbox" class="choosed-course" value="${window.allCourses[i].cour_id}"/></td>
      </tr>
    `
  }
  s += `</tbody>`
  showCourse_table.innerHTML += s;
}

//确认上传课程关系
function confimAddTeacherCourse() {
  if (!confirm("确认提交？")) return;
  //获得选择的课程
  let check_s = '';
  courseCheckBox = document.getElementsByClassName("choosed-course");
  for (let i = 0, len = courseCheckBox.length; i < len; ++i) {
    if (courseCheckBox[i].checked) {
      check_s += courseCheckBox[i].value + ',';
    }
  }
  //TODO 提交至后台，写入数据库 cotro/admin/addTeacherCourse.ashx
  $.post("cotro/admin/addTeacherCourse.ashx", {
    tid: window.tid,
    courses: check_s
  }, function (res, status) {
    if (res.line != "0") {
      alert("添加成功")
      location.reload();
    } else {
      alert("添加失败")
    }
  });
}

//删除一条教师课程关系
function deleteTeacherCourse(cour_id) {
  //TODO 删除 cotro/admin/deleteTeacherCourse.ashx
  $.post("cotro/admin/deleteTeacherCourse.ashx", {
    tid: window.tid,
    cour_id: cour_id
  }, function (res, status) {
    res = JSON.parse(res);
    if (res.line == "1") {
      alert("删除成功!")
      //TODO 成功的时候
      course_row = document.getElementById("teacherCourse-c" + cour_id);
      course_row.innerHTML = '';
      //------
    } else {
      alert("删除失败")
    }
  })
}